<?php
class MessengerController extends Controller {
    
    public function actionIndex(){
        $this->render('index');
    }
    
    /*Kiểm tra user người nhận*/
    public function actionCheckuser(){
        $username = isset($_POST['user_name'])?$_POST['user_name']:"";
        if($username != ""){
            $user = AdminUser::getRowByUserName($username);
            if(count($user) > 0){
                echo $user['id'];    
            }else{
                echo 0;
            }                
        }else{
            echo 0;
        }        
    }
    
    /*Ajax insert messenger*/
    public function actionInsertmessenger(){      
        $count = 0;
        $sender_id      = 0;
        $sender_name    = "Admin";
              
        $subject        = isset($_POST['title'])    ?$_POST['title']:"";
        $content        = isset($_POST['content'])  ?$_POST['content']:"";
        $user_id        = isset($_POST['user_id'])  ?intval($_POST['user_id']):"";
        $user_name      = isset($_POST['user_name'])?$_POST['user_name']:"";
        
        $messenger = AdminMessengerBox::insertRow($sender_id,$subject,$content,$sender_name,$user_name);
        
        if($messenger > 0){          
            if($user_id == 0){
                $user = AdminUser::getRowsByUser();
                foreach($user as $row){
                    $messenger_user = AdminMessengerUser::insertRow($messenger,$row['id']);
                    if($messenger_user > 0){
                        $count++;    
                    }    
                }
            }else{
                $messenger_user = AdminMessengerUser::insertRow($messenger,$user_id);
                $count++;                              
            }
            echo $count;            
        }else{
            echo 0;
        }         
    }
   
    /*Trang thư đã gửi*/ 
    public function actionSend(){
        $url = new Url();
        $row = AdminMessengerBox::countAllRow();
        $count_row = $row['count'];
        
        $page = isset($_GET['page'])?intval($_GET['page']):1;
        $rows_per_page = 10;   
        $begin = ($page - 1)*$rows_per_page;                        
        $end = $rows_per_page;

        if($count_row % $rows_per_page == 0){
            $total_page = floor($count_row/$rows_per_page);
        } else{
            $total_page = floor($count_row/$rows_per_page) + 1;                             
        }
        
        $messenger = AdminMessengerBox::getAllRowPaging($begin,$end);
        $page = Paging::showPageNavigationMore($page,$total_page,$url->createUrl('messenger/send')."?");
        $count = count($messenger);
        $this->render('send',array('count'=>$count,'data'=>$messenger,'page'=>$page,'begin'=>$begin));
    }
    
    /*Ajax xóa thư*/
    public function actionDeleterow(){
        $id = isset($_POST['id'])?$_POST['id']:"";
        $arr_id = rtrim($id,",");
        $delete = AdminMessengerBox::deleteMutilRow($arr_id);
        if($delete > 0){
            $del_mesUser = AdminMessengerUser::deleteRow($arr_id); 
            if($del_mesUser > 0){
                echo $del_mesUser;   
            }else{
                echo $delete;
            }   
        }else{
            echo 0;
        }               
    }
    
    /*Trang chi tiết thư*/
    public function actionDetail(){
        $id     = isset($_GET['id'])    ? intval($_GET['id'])   : 0;
        if($id > 0){
            $mes = AdminMessengerBox::getRowById($id);
            $this->render("detail",array("mes"=>$mes));           
        }        
    }
}